﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Windows.Forms;

namespace Arthus
{
    class KPR
    {
        MySqlConnection konekcija;
        MySqlCommand komanda = new MySqlCommand();
        MySqlDataReader datareader = null;
        MySqlDataAdapter adapter = null;
        DataTable dataTable = null;

        public void KPRPopuniGridView(String combo, ComboBox comboBoxPreduzeceKPR, DataGridView dataGridViewKPR)
        {
            if (!comboBoxPreduzeceKPR.SelectedItem.ToString().Equals(null) || comboBoxPreduzeceKPR.SelectedItem.ToString() != null ||
                comboBoxPreduzeceKPR.SelectedItem.ToString() != "")
            {
                //kreiranje objekata pojedinih klasa
                Partneri partnerObjekat = new Partneri();
                FIN FINobjekat = new FIN();
                Preduzece preduzeceObjekat = new Preduzece();
                try
                {
                    //ciscenje i spremanje datagridview-a
                    dataGridViewKPR.DataSource = null;
                    dataGridViewKPR.Rows.Clear();
                    dataGridViewKPR.Refresh();

                    //konekcija sa bazom i punjenje datatable-a
                    konekcija = new MySqlConnection(statickePromenljive.konekcioniString);
                    komanda.Connection = konekcija;
                    komanda.CommandText = "SELECT KPRID, DATE_FORMAT(KPRDKI, '%d.%m.%Y.'), KPRNAL, KPRVRS, KPRBRR, DATE_FORMAT(KPRDAR, '%d.%m.%Y.')," +
                    " KPRPAR, KPRKON, KPRP08 FROM KPR WHERE KPRPRE=" + preduzeceObjekat.VratiIDPreduzecaNaOsnovuOznake(combo) + ";";
                    dataTable = new DataTable();
                    adapter = new MySqlDataAdapter();
                    adapter.SelectCommand = komanda;
                    adapter.Fill(dataTable);

                    adapter.Dispose();
                    konekcija.Close();

                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        dataGridViewKPR.Rows.Add(dataTable.Rows[i][0], dataTable.Rows[i][1], FINobjekat.VratiOznakuNaOsnovuID((int)dataTable.Rows[i][2]),
                            dataTable.Rows[i][3], dataTable.Rows[i][4], dataTable.Rows[i][5], partnerObjekat.VratiOznakuPartneraNaOsnovuID((int)dataTable.Rows[i][6]),
                            dataTable.Rows[i][7], dataTable.Rows[i][8]);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Neočekivana greška! \n" + ex.ToString(), "Greška!", MessageBoxButtons.OK);
                }
                finally
                {
                    dataTable.Clear();
                    adapter.Dispose();
                    konekcija.Close();
                }
            }
            else
            {
                //ukoliko nije nista odabrano
            }
        } //metoda koja puni dataGridViewKPR podacima iz baze
    }
}
